﻿/* 06 Write a program that reads two integer numbers N and K
 * and an array of N elements from the console.
 * Find in the array those K elements that have maximal sum. */

using System;

    class MaxSumOfElements
    {
        static void Main()
        {
            Console.Write("Please enter the size of the array: ");
            int n = int.Parse(Console.ReadLine());
            int[] array = new int[n];
            Console.Write("Please enter the number of element with MAX sum: ");
            int k = int.Parse(Console.ReadLine());
            //reading the array
            for (int index = 0; index < array.Length; index++)
            {
                Console.Write("Array[{0}]= ",index);
                array[index] = int.Parse(Console.ReadLine());                
            }
            int maxSum = 0;
            int tempSum = 0;
            int currentPosition = 0;
            int bestStartPosition = 0;
            //finding the array with K-elements and with MAX sum
            for (int i = 0; i < array.Length-k+1; i++)
            {
                currentPosition = i;
                for (int l = 0; l < k; l++)
                {
                    tempSum += array[i+l];
                }              
                if (tempSum>maxSum)
                {
                    maxSum = tempSum;
                    bestStartPosition = currentPosition;
                }
                tempSum = 0;
            }
            //printing the array with K-element and with MAX sum
            Console.WriteLine("The array with maximum sum is: ");
            Console.Write("{");
            for (int j= 0;  j < k; j++)
            {
                Console.Write(array[bestStartPosition+j]+((j+1==k)? "":", "));
             
            }
            Console.WriteLine("}");
        }
    }

